package src 
{
	/**
	 * ...
	 * @author Vitaly Vrublevskyy
	 */
	public class Grid
	{
		private var _source:Array;
		public function get source():Array 
		{
			return _source;
		}
		
		public function set source(value:Array):void 
		{
			_source = value;
		}
		
		public function iniialize():void
		{
			do
			{
			    randomizedFill();
				trace("Tree generate unique")
			} 
			while ( existMatches() || !posibleMove() )
				
		}
		
		
		private function randomizedFill():void 
		{
			source  = []; //reset
			for (var i:int = 0; i < Constant.COLUMNS; i++) 
			{
				source[i] = [];
				for (var j:int = 0; j < Constant.COLUMNS; j++) 
				{
					source[i][j] = int(Math.random() * Constant.ENTITY_KINDS + 1);
				}
			}
			
			trace( source.join("\n") );
		}
		
		
		
		private function existMatches():Boolean 
		{
			//analise vertical lines
			for (var column:int = 0; column < Constant.COLUMNS; column++) 
			{
				for (var row:int = 0; row < Constant.ROWS - 2; row++) 
				{
					if ( equalVertical(column, row).length > 2 )
					{
						//TODO: shuffle midle (simple change)
						return true;
					}
				}
			}			
			
			//analise horizontal lines
			for (var row:int = 0; row < Constant.ROWS; row++) 
			{
				for (var column:int = 0; column < Constant.COLUMNS - 2; column++) 
				{
					if (equalHorizontal(column, row).length > 2)
					{
						return true;
					}
				}
			}
			
			
			return false;
		}
		
		
		private function posibleMove():Boolean 
		{
			trace("MEthod 2");
			return true;
		}
		
		
		private function equalHorizontal(column:uint, row:uint):Array
		{
			var result:Array = new Array( source[column][row] );
			for (var i:int = 1; column + i < Constant.COLUMNS; i++) 
			{
				if (  source[column][row] ==  source[column + i][row]) 
				{
					result.push(source[column + i][row])
				}
				else 
				{
					return result;
				}
			}
			
			return result;
		}
		
		private function equalVertical(column:uint, row:uint):Array
		{
			var result:Array = new Array( source[column][row] );
			for (var i:int = 1; row + i < Constant.ROWS; i++) 
			{
				if (  source[column][row] ==  source[column][row + i]) 
				{
					result.push(source[column][row + i])
				}
				else 
				{
					return result;
				}
			}
			
			return result;
		}
	}
	


}